package controllers

import play.api.libs.json.Json
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}
import tool.{FormTool, Tool}

import java.io.File
import javax.inject.Inject
import implicits.Implicits._
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 8/5/2021
 */
class DetailController @Inject()(cc: ControllerComponents)(
) extends AbstractController(cc) {

  def detailInfo: Action[AnyContent] = Action { implicit request =>
    val data = FormTool.idForm.bindFromRequest().get
    val kind = FormTool.kindForm.bindFromRequest().get.kind
    val tabOp = FormTool.tabOpForm.bindFromRequest().get.tab
    Ok(views.html.search.detail(data.id, kind, tabOp))
  }

  def getAllDataset: Action[AnyContent] = Action { implicit request =>
    val array = List("Urine", "CSF", "Bile", "Healthy", "Benign", "BRCA", "CHD", "CRC", "ESCC", "GBM", "GC", "HCC", "KIRC", "ML", "MEL", "OV", "PAAD", "SCLC")
    Ok(Json.toJson(array))
  }

  def downloadPlotData = Action { implicit request =>
    val id = FormTool.idForm.bindFromRequest().get.id
    val kind = FormTool.kindForm.bindFromRequest().get.kind
    val file = Tool.getTargetFile(kind, id)
    Ok.sendFile(file).withHeaders(
      CONTENT_DISPOSITION -> ("attachment; filename=" + file.getName),
      CONTENT_TYPE -> "application/x-download"
    )
  }


}
